/*
 *  FireWallList.c
 *  FireWall
 *
 *  Created by Admin on 8/01/13.
 *  Copyright (c) 2013 Admin. All rights reserved.
 *
 */
#include "FireWallList.h"

char *tagName = "OSMtag";
value_t mKey[mkSize]={0};

struct list *new_list(void) {
    OSMallocTag tag = OSMalloc_Tagalloc(tagName, OSMT_DEFAULT);
    struct list *res = (struct list*) OSMalloc(sizeof(struct list), tag);
    res->tag = tag;
    res->first = NULL;
    res->size = 0;
    return res;
}

struct li* new_list_item(void) {
    OSMallocTag tag = OSMalloc_Tagalloc("LiTag",OSMT_DEFAULT);
    struct li *res = (struct li*) OSMalloc(sizeof(struct li), tag);
    res->tag = tag;
    res->next = 0;
    return res;
}

void delete_items(struct li* it) {
    if ( it ) {
        if ( it->next ) {
            delete_items(it->next);
        }
        OSMallocTag tag = it->tag;
        OSFree(it, sizeof(struct li), tag);
        OSMalloc_Tagfree(tag);
    }
}

void delete_list(struct list *l) {
    if ( l ) {
        delete_items( l->first );
        OSMallocTag tag = l->tag;
        OSFree(l, sizeof(struct list),tag);
        OSMalloc_Tagfree(tag);
    }
}

int insert_item( struct list *l,  value_t value[] ) {
    struct li *it = new_list_item();
    if ( it == NULL ) return 0;
    for (int i=0;i<mkSize;i++)
        it->value[i] = value[i];
    it->next = l->first;
    l->first = it;
    l->size++;
    return 1;
}

int insert_item_uniq( struct list *l, char path[] ) {
    if ( l ) {
        int zero=0;
        writeMagicKey(path, mKey);
        for (int i=0;i<mkSize;i++){
            if(mKey[i]>0) zero++;
        }
        if(zero>0){
            struct li *it;
            for ( it = l->first; it != NULL; it = it->next ) {
                if(foo(it->value, mKey)){
                    for (int i=0;i<mkSize;i++)
                        it->value[i] = mKey[i];
                    return 1;
                }
            }
            return insert_item(l,  mKey);
        }
        else return 0;
    } else {
        return 0;
    }
}

int delete_item( struct list *l,char path[] ) {
    struct li *it, *prev_it = 0;
    
    writeMagicKey(path, mKey);
    for (  it = l->first; it != NULL; prev_it = it, it = it->next ) {
        
        if(foo(it->value, mKey)){
            if ( prev_it != 0 )
                prev_it->next = it->next;
            else
                l->first = it->next;
            
            OSMallocTag tag = it->tag;
            OSFree(it, sizeof(struct li), tag);
            OSMalloc_Tagfree(tag);
            l->size--;
            return 1;
        }
    }
    return 0;
}

void print_list ( struct list *l ) {
    struct li *it;
    printf( "Size=%d\n", l->size );
    for ( it = l->first; it != NULL; it = it->next ) {
        for (int i=0;i<mkSize;i++)
            printf("%d ",it->value[i]);
    }
    printf( "NULL\n" );
}

boolean_t find_item( struct list *l, value_t value[]) {
    struct li *it;
    for ( it = l->first; it != NULL; it = it->next ) {
        if ( foo(it->value, value) ) {
            return 1;
        }
    }
    return 0;
}

void writeMagicKey(char path[], value_t blockKey[]){
    
    char adr[readBuff]= {0};
    struct uuid_command *uid = NULL;
    
    vnode_t vn;
    vfs_context_t vfs = vfs_context_current();
    
    if(strlen(path)>2){
        
        if (vnode_open(path, O_RDONLY, (0), 0, &vn, vfs) == 0) {
            uio_t read;
            read = uio_create(1, 0, UIO_SYSSPACE, UIO_READ);
            uio_addiov(read, (user_addr_t)adr, readBuff);
            
            VNOP_READ(vn, read,  0, vfs);
            
            
            uid = (struct uuid_command*)adr;
            for(int i =0; i<mkSize;i++){
                blockKey[i]=uid->uuid[i];
            }
            
            uio_free(read);
            vnode_close(vn, 0, vfs);
        } else {       
        }
        for(int i =0; i<mkSize;i++){
            printf("%d",blockKey[i]);
        }
        printf("\n");
    }
}

boolean_t foo(value_t arg1[],uint8_t arg2[]){
    for (int i=0;i<mkSize;i++)
        if(arg1[i]!=arg2[i]) return 0;
    return  1;
    
}
